package danny.hoi.web.shiro;

import danny.hoi.web.utils.Md5Utils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

import java.util.Objects;

public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        //获取登陆的账号密码
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String loginEmail = (String) upToken.getPrincipal();
        String loginPassword = new String(upToken.getPassword());

        //账号加密加盐
        String md5Password = Md5Utils.getMd5(loginPassword,loginEmail);

        //数据库中的密码
        String dbPassword = (String) info.getCredentials();

        //返回匹配结果
        return Objects.equals(dbPassword,md5Password);
    }

}
